//*************************************************************************************
/** \file ser_string.h
 *    This file contains a class which allows use of the set of overloaded left-shift 
 *    operators "<<" in base_text_serial.* to easily put all sorts of data into a 
 *    character buffer in a manner similar to strstreams in regular C++. 
 *
 *  Revised:
 *    \li 04-03-2006 JRR For updated version of compiler
 *    \li 06-10-2006 JRR Ported from C++ to C for use with some C-only projects; also
 *        serial_avr.h now has defines for compatibility among lots of AVR variants
 *    \li 08-11-2006 JRR Some bug fixes
 *    \li 03-02-2007 JRR Ported back to C++. I've had it with the limitations of C.
 *    \li 04-16-2007 JO  Added write (unsigned long)
 *    \li 07-19-2007 JRR Changed some character return values to bool, added m324p
 *    \li 01-12-2008 JRR Added code for the ATmega128 using USART number 1 only
 *    \li 02-14-2008 JRR Split between base_text_serial and rs232 files
 *    \li 05-31-2008 JRR Changed baud calculations to use CPU_FREQ_MHz from Makefile
 *    \li 06-01-2008 JRR Added getch_tout() because it's needed by 9Xstream modems
 *    \li 07-05-2008 JRR Changed from 1 to 2 stop bits to placate finicky receivers
 *    \li 12-22-2008 JRR Split off stuff in base232.h for efficiency
 *    \li 06-30-2009 JRR Received data interrupt and buffer added
 *    \li 11-20-2011 JRR Changed from serial port driver to string buffer filler upper
 *
 *  License:
 *    This file copyright 2006-2011 by JR Ridgely. It is released under the Lesser GNU
 *    public license, version 2. It is intended for educational use only, but it may
 *    be used for any purpose permitted under the LGPL. The author has no control 
 *    over the uses of this software and cannot be responsible for any consequences 
 *    of such use. 
 */
//*************************************************************************************

/// This define prevents this .h file from being included more than once in a .cc file
#ifndef _SER_STRING_H_
#define _SER_STRING_H_

#include "base_text_serial.h"				// Pull in the base class header file


/// This is the number to which we count before timing out waiting for buffer space.
#define SERST_TIMEOUT		20000


//-------------------------------------------------------------------------------------
/** This class allows the overloaded shift operators "<<" from base_text_serial.h/cpp
 *  to be used to write text, numbers, and so on conveniently into a character buffer.
 *  The character buffer is configured as a queue (that is, a ring buffer) so that the
 *  first characters written in are the first characters read out, and the buffer can
 *  be used to hold characters which are made into packets or sent out through a slow
 *  port (for example, a serial port) by another task or an interrupt service routine.
 */

class ser_string : public base_text_serial
{
	// This protected data can only be accessed from this class or its descendents
	protected:
		char* p_buffer;					///< Pointer to the buffer which holds chars
		uint16_t size;					///< Number of characters in the buffer
		uint16_t i_put;					///< Index where newest data will be written
		uint16_t i_get;					///< Index where oldest data was written
		uint16_t how_full;				///< How many elements are full at this time

	// Public methods can be called from anywhere in the program where there is a 
	// pointer or reference to an object of this class
	public:
		// The constructor sets up the string writer, given a pointer to a buffer
		ser_string (char*, uint16_t);

		/// This method writes one character to the serial port.
		bool putchar (char);

		void puts (char const*);			// Write a string constant to serial port
		bool check_for_char (void);			// Check if a character is in the buffer
		int16_t getchar (void);				// Get a character; wait if none is ready

		char operator[] (uint16_t);			// Operator returns character at an index
};

#endif  // _SER_STRING_H_
